package com.woniuxy.util;

import com.woniuxy.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: tsm
 * @description:
 * @create: 2024-10-23 16:59
 **/
@Component
@Aspect
public class MyAspect {
    @Resource
    LogService logService;
    private long startTime;
    private long endTime;
    private final String POINTCUT = "execution(* com.woniuxy.service.impl.*.*(..))";


    // 映射方法名到中文描述
    private static final Map<String, String> methodNameMapping = new HashMap<>();
    // 映射类名到中文描述
    private static final Map<String, String> classNameMapping = new HashMap<>();

    static {
        classNameMapping.put("com.woniuxy.service.impl.YiServiceImpl", "在途监控");
        classNameMapping.put("com.woniuxy.service.impl.OrderServiceImpl", "订单管理");
        classNameMapping.put("com.woniuxy.service.impl.ProvincialServiceImpl", "订单管理");
        classNameMapping.put("com.woniuxy.service.impl.WaybillServiceImpl", "运单管理");
        classNameMapping.put("com.woniuxy.service.impl.DeptServiceImpl", "部门管理");
        classNameMapping.put("com.woniuxy.service.impl.GoodsManagementServiceImpl", "货物管理");
        classNameMapping.put("com.woniuxy.service.impl.AddressManagementServiceImpl", "地址管理");
        classNameMapping.put("com.woniuxy.service.impl.DriverManagementServiceImpl", "司机管理");
        classNameMapping.put("com.woniuxy.service.impl.PartnerServiceImpl", "合作商管理");
        classNameMapping.put("com.woniuxy.service.impl.CustomerManagementServiceImpl", "客户/用户管理");
        classNameMapping.put("com.woniuxy.service.impl.SignOrderServiceImpl", "签收单管理");
        classNameMapping.put("com.woniuxy.service.impl.ReturnOrderServiceImpl", "回单管理");
        classNameMapping.put("com.woniuxy.service.impl.PickupOrderServiceImpl", "提货单管理");
        classNameMapping.put("com.woniuxy.service.impl.CarServiceImpl", "车辆管理");
        classNameMapping.put("com.woniuxy.service.impl.FapiaoServiceImpl", "发票管理");
        classNameMapping.put("com.woniuxy.service.impl.LogisticsPartnerServiceImpl", "合作商管理");
        classNameMapping.put("com.woniuxy.service.impl.PowerUserServiceImpl", "员工资料管理");
        classNameMapping.put("com.woniuxy.service.impl.ReceivableOrderServiceImpl", "应收单管理");
    }

    @Around(POINTCUT)
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        startTime = System.currentTimeMillis();
        String className = joinPoint.getSignature().getDeclaringTypeName();
        String methodName = joinPoint.getSignature().getName();
//        String allName=className+"."+methodName;

        // 获取中文描述
//        String methodDescription = methodNameMapping.getOrDefault(methodName, methodName);
        String classNameOrDefault = classNameMapping.getOrDefault(className, className);

        Object proceed = joinPoint.proceed();

        endTime = System.currentTimeMillis();
        long duringTime = endTime - startTime;

        if (!className.contains("Log") && !className.contains("PowerUser") && !methodName.contains("login")
                && !className.contains("Menu") && !methodName.contains("queryException2")) {

            logService.op(duringTime, classNameOrDefault, methodName);

        }
        return proceed;
    }
}
